//
//  MainVC_Work_ButtonsCard.m
//  liBaQiang
//
//  Created by 奇林刘 on 2018/10/9.
//  Copyright © 2018年 liu. All rights reserved.
//


#import "MainVC_Work_ButtonsCard.h"
#import "MainVC_Work_Button.h"

@interface MainVC_Work_ButtonsCard ()

@property (nonatomic, strong) UILabel *title;

@end

@implementation MainVC_Work_ButtonsCard {
    NSString *_titleString;
    NSMutableArray<MainVC_Work_Button *> *_buttonArray;
}

#pragma mark - lifeCycle
- (instancetype)initWithButtonDescriptions:(NSArray<NSDictionary *> *)descriptions title:(NSString *)title {
    [self fillVariablesWithButtonDescriptions:descriptions title:title];
    self = [super init];
    return self;
}

- (void)configSelf {
    self
    .fp_backgroundColor([UIColor whiteColor])
    .fp_cornerRadius(RPX(4))
    .fp_shadowOffset(CGSizeMake(0, RPX(3)))
    .fp_shadowRadius(RPX(4))
    .fp_shadowColor(HexadecimalColor(@"#0084FF"))
    .fp_shadowOpacity(0.08);
    
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(SelfWidth));
    }];
}

- (void)allocateComponents {
    [self allocateTitle];
    [self placeButtons];
}

- (void)configComponents {
    [self configTitle];
}

#pragma mark - usedMethods
- (void)fillVariablesWithButtonDescriptions:(NSArray<NSDictionary *> *)descriptions title:(NSString *)title {
    _buttonArray = [NSMutableArray array];
    for (NSDictionary *description in descriptions) {
        NSString *image = description[@"image"];
        NSString *title = description[@"title"];
        MainVC_Work_Button *button = [[MainVC_Work_Button alloc] initWithImageName:image title:title];
        [button addTarget:[HandyMethods getViewControllerForView:self] action:NSSelectorFromString(description[@"method"]) forControlEvents:UIControlEventTouchUpInside];
        [_buttonArray addObject:button];
    }

    _titleString = title;
}

- (void)placeButtons {
    for (int i=0; i<_buttonArray.count; i++) {
        MainVC_Work_Button *currentButton = _buttonArray[i];
        MainVC_Work_Button *previousButton = (i==0) ? nil : _buttonArray[i-1];
        [self addSubview:currentButton];
        [currentButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self).multipliedBy(1.0/CountPerRow);
            if (i % CountPerRow == 0) {
                if (i == 0) {
                    make.top.equalTo(self.title.mas_bottom).offset(SpaceBetweenLines);
                } else {
                    make.top.equalTo(self->_buttonArray[i-CountPerRow].mas_bottom).offset(SpaceBetweenLines);
                }
                make.left.equalTo(self);
            } else {
                make.centerY.equalTo(previousButton);
                make.left.equalTo(previousButton.mas_right);
            }
            if (i == self->_buttonArray.count - 1) {
                make.bottom.equalTo(self).offset(-SpaceBetweenLines);
            }
        }];
    }
}

//allocate
- (void)allocateTitle {
    self.title = [[UILabel alloc] init];
    [self addSubview:self.title];
    [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(SpaceBetweenLines);
        make.left.equalTo(self).offset(RPX(15));
    }];
}

//config
- (void)configTitle {
    self.title
        .fp_text(_titleString)
        .fp_font(RPXFont(16))
        .fp_textColor([UIColor blackColor]);
}



@end
